<?php

/**
 * education_background actions.
 *
 * @package    erp-dnict
 * @subpackage education_background
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class education_backgroundActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  }

  public function executeEducation(sfWebRequest $request)
  {
  	$q = Doctrine_Query::create()
  			->from('EducationBackground eb')
  			->where('eb.education_type = ?', EducationBackground::TYPE_EDUCATION)
        ->addWhere('eb.is_active = ?', true);
  	$this->educations = $q->execute();
  	return sfView::SUCCESS;
  }

  public function executePolitic(sfWebRequest $request)
  {
  	$q = Doctrine_Query::create()
  			->from('EducationBackground eb')
  			->where('eb.education_type = ?', EducationBackground::TYPE_POLITIC)
        ->addWhere('eb.is_active = ?', true);
  	$this->educations = $q->execute();
  	$this->setTemplate('education');
  	return sfView::SUCCESS;
  }

  public function executeLanguage(sfWebRequest $request)
  {
  	$q = Doctrine_Query::create()
  			->from('EducationBackground eb')
  			->where('eb.education_type = ?', EducationBackground::TYPE_LANGUAGE)
        ->addWhere('eb.is_active = ?', true);
  	$this->educations = $q->execute();
  	$this->setTemplate('education');
  	return sfView::SUCCESS;
  }

  public function executeEdit(sfWebRequest $request)
  {
  	$this->education_id = $request->getParameter('id', null);
  	$this->form = new EducationBackgroundForm();

  	if ($this->education_id) {
  		$education = Doctrine_Core::getTable('EducationBackground')->find($this->education_id);
  		$this->form = new EducationBackgroundForm($education);
  	}

    if ($request->isMethod('post')) {
      $form_data = $request->getParameter('education_background');
      $this->form->bind($form_data);
      if ($this->form->isValid()) {
        if (!$request->getParameter('id'))
          $education = $this->form->save();
        else { // update
          $education = Doctrine_Core::getTable('EducationBackground')->find($request->getParameter('id'));
          if ($education) {
            $education->education_type = $form_data['education_type'];
            $education->name = $form_data['name'];
            $education->description = $form_data['description'];
            $education->is_active = $form_data['is_active'] == 'on' ? true : false;
            $education->save();
          }
        }
      }
    }

  	return sfView::SUCCESS;
  }

  public function executeRemove(sfWebRequest $request)
  {
    $education_id = $request->getParameter('id', null);
    if ($education_id) {
      $education = Doctrine_Core::getTable('EducationBackground')->find($education_id);
      if ($education) {
        foreach ($education->Staffs as $staff) {
          $staff->delete();
        }
        $education->is_active = false;
        $education->save();
        if ($education->education_type == EducationBackground::TYPE_EDUCATION)
          $this->redirect('@education_background_index');
        elseif ($education->education_type == EducationBackground::TYPE_POLITIC)
          $this->redirect('@politic_background_index');
        else
          $this->redirect('@language_background_index');
      }
    }
  }
}
